package string.t541;

public class One {

    public String reverseStr(String s, int k) {

        // 还是双指针
        char[] charArray = s.toCharArray();

        // 首先找到需要转换的数组
        swapS(charArray, 0, k - 1, k);

        return String.valueOf(charArray);

    }

    private void swapS(char[] charArray, int begin, int end, int k) {
        if (begin > charArray.length - 1) {
            return;
        }
        end = end > charArray.length - 1 ? charArray.length - 1 : end;
        if (begin >= end) {
            return;
        }
        turnOver(charArray, begin, end);
        swapS(charArray, end + k + 1, end + 2 * k, k);
    }

    private void turnOver(char[] charArray, int i, int j) {
        while (i < j) {
            char temp = charArray[i];
            charArray[i] = charArray[j];
            charArray[j] = temp;
            i++;
            j--;
        }
    }
}
